package com.wfm.leetcode.editor.cn;

import com.wfm.common.ListNode;

public class P剑指Offer18删除链表的节点{
//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode deleteNode(ListNode head, int val) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode pre = dummy;
        while (head!=null){
            if(head.val==val){
                pre.next=head.next;
                break;
            }else {
                pre=pre.next;
                head=head.next;
            }
        }
        return dummy.next;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

    public static void main(String[] args) { 
        Solution solution = new P剑指Offer18删除链表的节点().new Solution();
        ListNode listNode = ListNode.generalListNode(new int[]{4, 5, 1, 9});
        ListNode listNode1 = solution.deleteNode(listNode, 4);
        ListNode.printListNode(listNode1);
    }
}